package com.hb.repository.impl;

import com.hb.repository.SpitterSweeper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * 注意，SpitterRepositoryImpl 并没有实现 SpitterRepository 接口。Spring Data JPA 负责实现这个接口。SpitterRepositoryImpl
 * （将它与 Spring Data 的 Repository 关联起来的是它的名字）实现了 SpitterSweeper 接口
 */
public class SpringDataSpitterRepositoryImpl implements SpitterSweeper {
    @PersistenceContext
    private EntityManager em;

    /**
     * 我们还需要确保 eliteSweep() 方法会被声明在 SpitterRepository 接口中。要实现这一点，避免代码重复的简单方式就是修改
     * SpitterRepository，让它扩展 SpitterSweeper
     * @return
     */
    @Override
    public int eliteSweep() {
        String update = "UPDATE Spitter spitter SET spitter.status = 'Elite' WHERE spitter.status = 'Newbie'" +
                " AND spitter.id IN (SELECT s FROM Spitter s WHERE (SELECT COUNT(spittles) FROM s.spittles spittles) > 10000) ";
        return em.createQuery(update).executeUpdate();
    }
}
